package bjx.com.kehu.application;

import android.app.Application;
import android.app.Service;
import android.os.Vibrator;

import com.baidu.mapapi.SDKInitializer;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.engine.Engine;
import bjx.com.kehu.service.LocationService;
import bjx.com.kehu.utils.SPUtils;
import bjx.com.kehu.utils.Util;
import com.lzy.okgo.OkGo;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class App extends Application {
    private static App sInstance;
    private Engine mEngine;

    public LocationService locationService;
    public Vibrator mVibrator;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
//        CrashHandler crashHandler  = CrashHandler.getInstance();
//        crashHandler.init(getApplicationContext()); //在Appliction里面设置我们的异常处理器为UncaughtExceptionHandler处理器

        mEngine = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(Engine.class);

        /***
         * 初始化定位sdk，建议在Application中创建
         */
        locationService = new LocationService(getApplicationContext());
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        SDKInitializer.initialize(getApplicationContext());


        OkHttpUtils.getInstance()
                .init(this)
                .debug(true, "okHttp")
                .timeout(20 * 1000);
        OkGo.getInstance().init(this);
    }

    public static App getInstance() {
        return sInstance;
    }

    public Engine getEngine() {
        return mEngine;
    }

    /**
     * 封装公共参数
     * <p>
     */
    public class CommonInterceptor implements Interceptor {

        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request oldRequest = chain.request();

            String random = String.valueOf(new Random().nextInt(10000));
            // 添加新的参数
            HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                    .newBuilder()
                    .scheme(oldRequest.url().scheme())
                    .host(oldRequest.url().host())
                    .addQueryParameter(Constants.PUBLIC_PARAM1, random)
                    .addQueryParameter(Constants.PUBLIC_PARAM2, String.valueOf(SPUtils.get(getInstance(), "fid", "-1")))
                    .addQueryParameter(Constants.PUBLIC_PARAM3, Util.getVersion(getInstance()))
                    .addQueryParameter(Constants.PUBLIC_PARAM4, "android")
                    .addQueryParameter(Constants.PUBLIC_PARAM5, Util.getPhoneSign(getInstance()))
                    .addQueryParameter(Constants.PUBLIC_PARAM6, String.valueOf(SPUtils.get(getInstance(), Constants.CHANNELID, "-1")));

            // 新的请求
            Request newRequest = oldRequest.newBuilder()
                    .method(oldRequest.method(), oldRequest.body())
                    .url(authorizedUrlBuilder.build())
                    .build();

            return chain.proceed(newRequest);
        }
    }

    public OkHttpClient getOkHttpClient() {
        Interceptor interceptor = new CommonInterceptor();
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .addInterceptor(interceptor)
                .build();
        return client;
    }

}